package net.zjitc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.zjitc.domain.User;
import net.zjitc.domain.VO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    //不常用
    @RequestMapping( "/quick5")
    public String save5(HttpServletRequest request){
        request.setAttribute("msg","hello mvc");
        return "success";
    }

    @RequestMapping( "/quick4")
    public String save4(Model model){
        model.addAttribute("msg","hello mvc");
        return "success";
    }
    @RequestMapping( "/quick3")
    public ModelAndView save3(ModelAndView mv){
        mv.addObject("msg","hello mvclalala");
        mv.setViewName("success");
        return mv;
    }
    @RequestMapping( "/quick2")
    public ModelAndView save2(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","hello mvc");
        mv.setViewName("success");
        return mv;
    }
    @RequestMapping( "/quick")
    public String save(){
        System.out.println("controller running");
        return "success";
    }



    //回写
    @RequestMapping( "/quick6")
    public void save6(HttpServletResponse response)throws IOException{
        response.getWriter().print("hello mvc");
    }
    @RequestMapping( "/quick7")
    @ResponseBody  //不进行页面跳转 直接回写
    public String save7()throws IOException {
        return "hello mvc";
    }

    @RequestMapping( "/quick8")
    @ResponseBody
    public String save8() throws IOException {
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }
    @RequestMapping( "/quick9")
    @ResponseBody
    public String save9 () throws IOException {
        User user = new User();
        user.setName("lisi");
        user.setAge(18);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(user);
    }
    @RequestMapping( "/quick10")
    @ResponseBody
    public User save10 () throws IOException {
        User user = new User();
        user.setName("wangwu");
        user.setAge(18);
        return user;
     }
     /*// 请求参数封装*/
    // 1. 基本类型
     @RequestMapping( "/quick11")
     @ResponseBody
     public void save11 (String name , int age) throws IOException {
         System.out.println(name);
         System.out.println(age);//浏览器空白 参数在控制台
     }
    // 2. POJO
    @RequestMapping( "/quick12")
    @ResponseBody
    public void save12(User user) throws IOException {
        System.out.println(user);
    }
    //  3. 数组
    @RequestMapping( "/quick13")
    @ResponseBody
    public void save13(String[] strs) throws IOException {
        System.out.println(Arrays.asList(strs));
    }
    // 4. 集合
    //4.1
    @RequestMapping( "/quick14")
    @ResponseBody
    public void save14(VO vo) throws IOException {
        System.out.println(vo);
    }
    //4.2
    @RequestMapping( "/quick15")
    @ResponseBody
    public void save15(@RequestBody List<User> userList) throws IOException {
         System.out.println(userList);
    }


    @RequestMapping( "/quick16")
    @ResponseBody
    public void save16(@RequestParam(value = "username",required =false,defaultValue = "zhangsan") String name) throws IOException {
        System.out.println(name);
    }

    @RequestMapping( "/quick17/{username}")
    @ResponseBody
    public void save17(@PathVariable(value = "username",required =true) String name) throws IOException {
        System.out.println(name);
    }


    @RequestMapping( "/quick18")
    @ResponseBody
    public void save18(Date date) throws IOException {
        System.out.println(date);
    }
    /*这部分视频离奇消失*/
    @RequestMapping( "/quick19")
    @ResponseBody
    public void save19(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }
    @RequestMapping( "/quick20")
    @ResponseBody
    public void save20(@RequestHeader(value = "User-Agent",required = false) String user_agent) throws IOException {
        System.out.println(user_agent );
    }
    @RequestMapping( "/quick21")
    @ResponseBody
    public void save21(@CookieValue(value="JSESSIONID") String jsessionId) throws IOException {
        System.out.println(jsessionId );
    }
}




